// ignore_for_file: camel_case_types

import 'package:aitao_flutter/request/api.dart';
import 'package:aitao_flutter/routes/routes.dart';
import 'package:aitao_flutter/utils/alert_utils.dart';
import 'package:aitao_flutter/utils/log_utils.dart';
import 'package:aitao_flutter/utils/sp_utils.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:nim_core/nim_core.dart';

import '../const/common_const.dart';
import '../request/requestManager.dart';

typedef sendMessageCallback = void Function(
    NIMMessageType type, int filter, String content);

class IMPayTool {
  static sendMessageFilter(NIMMessageType messageType, String content,
      String sessionId, sendMessageCallback sendMessageCallback) async {
    String type;
    if (messageType == NIMMessageType.text) {
      type = '1'; // 文本
    } else if (messageType == NIMMessageType.audio) {
      type = '2'; // 语音
    } else if (messageType == NIMMessageType.image) {
      type = '3'; // 图片
    } else if (messageType == NIMMessageType.video) {
      type = '4'; // 视频
    } else {
      type = '100'; // 自定义
    }
    var result = await RequestManager().post(API.sendIM,
        params: {'to_uid': sessionId, 'type': type, 'content': content});
    debugPrint('发消息前的接口验证');
    if (result != null && result['code'] == 0) {
      Map data = result['data'];
      WJLogUtils.wjLog(result);
      // 暂时屏蔽
      // 是否通过检测
      // int checkStatus = data['check_status'];
      // if (checkStatus == 1) {
      //   EasyLoading.showToast(result['message']);
      //   return;
      // }
      //MARK: -- 这里可以设置ios的推送信息
      int isCut = data['is_cut']; // 0不扣费 1扣费
      int cutCoin = data['cut_coin']; // 扣除的金币数
      int coin = data['coin']; // 剩余金币数
      if (isCut == 1) {
        // 需要扣费
        if (SpUtil.getPayIMNotTip() == false) {
          // 消息不再提示
          String msg =
              '每条消息扣费$cutCoin${CommonStringConst.manCoinName},剩余:$coin${CommonStringConst.manCoinName}';
          AlertUtils().showImNotTipsDialog(Get.context!, msg, (str) {
            debugPrint('点击:$str');
            if (str == '立即认证') {
              Get.toNamed(Routes.certificationPage);
            } else if (str == '成为会员') {
              Get.toNamed(Routes.memberPage);
            }
          });
        }
      }
      int filter = data['filter']; // 是否过滤掉
      String callBackContent = data['content'].toString();
      sendMessageCallback(messageType, filter, callBackContent);
    }
  }

  /// 发送图片打招呼
  static sendGreetImage(
      String toUid, int imageId, VoidCallback sendSuccess) async {
    if (toUid == '' || imageId == 0) return;
    var result = await RequestManager()
        .post(API.sendGreetImage, params: {'to_uid': toUid, 'hi_id': imageId});
    debugPrint('发送图片打招呼result');
    WJLogUtils.wjLog(result);
    if (result != null && result['code'] == 0) {
      sendSuccess();
    }
  }

  /// 发送语音打招呼
  static sendGreetVoice(
      String toUid, int voiceId, VoidCallback sendSuccess) async {
    if (toUid == '' || voiceId == 0) return;
    var result = await RequestManager()
        .post(API.sendGreeVoice, params: {'to_uid': toUid, 'hi_id': voiceId});
    debugPrint('发送语音打招呼result');
    WJLogUtils.wjLog(result);
    if (result != null && result['code'] == 0) {
      sendSuccess();
    }
  }

  /// 发送普通文本消息
  static sendTextMessage(String sessionId, String content) async {
    var result = await MessageBuilder.createTextMessage(
        sessionId: sessionId, sessionType: NIMSessionType.p2p, text: content);
    debugPrint(result.isSuccess.toString());
    if (result.isSuccess) {
      debugPrint('发送消息');
      NimCore.instance.messageService
          .sendMessage(message: result.data!, resend: false);
    }
  }
}
